package com.kabuda.zhugehot200_review;

import java.util.Arrays;

/**
 * @author kabuda
 * @date 2025-03-29 15:51
 * @description
 */
public class 合并两个有序数组 {
    public static void main(String[] args) {
        int[] ints = {1, 2, 3, 0, 0, 0};
        merge(ints, 3, new int[]{1, 4, 6}, 3);
        System.out.println(Arrays.toString(ints));

    }

    public static void merge(int[] nums1, int m, int[] nums2, int n) {
        int[] tempNums1 = Arrays.copyOf(nums1, m);
        int index1 = 0;
        int index2 = 0;
        int index = 0;
        while (index1<m&&index2<n){
            if (tempNums1[index1] <= nums2[index2]) {
                nums1[index++] = tempNums1[index1++];
            } else {
                nums1[index++] = nums2[index2++];
            }
        }
        while (index2 < n) {
            nums1[index++] = nums2[index2++];
        }
        while (index1 < m) {
            nums1[index++] = tempNums1[index1++];
        }
    }
}
